package entities;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="team")
@NamedQueries({
	@NamedQuery(name="getListTeamByFlagActivatedByTournament",query="SELECT team FROM Team AS team WHERE team.oneFlag = :flag AND team.tournament = :tournament"),
	@NamedQuery(name="getCountTeamByTournamentByFlagActivated",query="SELECT COUNT(team) FROM Team AS team WHERE team.oneFlag = :flag AND team.tournament = :tournament")
})
public class Team extends EntityMother implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private long id;

	@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<Player> listPlayers;

	@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<Staff> listStaffMembers;

	@ManyToOne
	private Tournament tournament;

	private String name;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Set<Player> getListPlayers() {
		return listPlayers;
	}

	public void setListPlayers(Set<Player> listPlayers) {
		this.listPlayers = listPlayers;
	}

	public Set<Staff> getListStaffMembers() {
		return listStaffMembers;
	}

	public void setListStaffMembers(Set<Staff> listStaffMembers) {
		this.listStaffMembers = listStaffMembers;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Tournament getTournament() {
		return tournament;
	}

	public void setTournament(Tournament tournament) {
		this.tournament = tournament;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((tournament == null) ? 0 : tournament.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (tournament == null) {
			if (other.tournament != null)
				return false;
		} else if (!tournament.equals(other.tournament))
			return false;
		return true;
	}
}